﻿using System;
using System.IO;
using System.Xml.Serialization;

namespace DuJianliang.ApplicationPlatform.Common
{
    public static class XmlHelper
    {
        public static void Serialize(string fileFullName, object obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            try
            {
                XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType());
                Stream stream = new FileStream(fileFullName, FileMode.Create, FileAccess.Write, FileShare.Read);
                xmlSerializer.Serialize(stream, obj);
                stream.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static T Deserialize<T>(string fileFullName) where T : class, new()
        {
            try
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
                Stream stream = new FileStream(fileFullName, FileMode.Open, FileAccess.Read, FileShare.Read);
                object obj = xmlSerializer.Deserialize(stream);
                return obj as T;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
